const localStorage = window.localStorage;

const db = {

  set(key: string, value: any) {
    localStorage.setItem("crm_" + key, JSON.stringify(value))
  },

  get(key: string, defaultValue?: any) {
    const value = localStorage.getItem("crm_" + key)
    if (value !== null) {
      if (value[0] === '{' || '[') {
        return JSON.parse(value);
      }
      return value
    }
    return defaultValue
  },

  remove(key: string) {
    localStorage.removeItem("crm_" + key)
  },

  clear() {
    for (const key in localStorage) {
      localStorage.removeItem(key)
    }
  }
}

export default db;
